﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>

 <% 
     //ModelMetadata meta = ViewData.ModelMetadata;
     string masterTable = (string)ViewData["MasterTable"];
     string propertyName = (string)ViewData["PropertyName"];
     bool isRequired = Convert.ToBoolean(ViewData["IsRequired"]);
     string notSetName = (string)ViewData["nsn"];
     
     bool isEditFilter = false;
     if (ViewData["IsEditFilter"] != null)
     {
         isEditFilter = Convert.ToBoolean(ViewData["IsEditFilter"]);
     }

     bool isAjax = false;
     if (ViewData["IsAjax"] != null)
     {
         isAjax = Convert.ToBoolean(ViewData["IsAjax"]);
     }
      
     string onChangeFunctionName = (string)ViewData["ocf"];
     bool callOnchangeFuction = !string.IsNullOrEmpty(onChangeFunctionName);

     string dataFunctionName = Convert.ToString(ViewData["fn"]);
     string dataFunctionParameter = Convert.ToString(ViewData["p"]);

     List<KeyValuePair<string, string>> items = new List<KeyValuePair<string, string>>();

     if (!isAjax)
     {
         if (!string.IsNullOrEmpty(dataFunctionName))
         {
             items = Smartec.Accounts.Camel.AMS.Model.Facades.AMSManagerFacade.GetListItemsByMethodName(dataFunctionName, dataFunctionParameter);
         }
         else
         {
             items = Smartec.Accounts.Camel.AMS.Model.Facades.AMSManagerFacade.GetListItemsByTableName(masterTable);
         }
     }

     if (!isRequired)
         items.Insert(0, new KeyValuePair<string, string>("", string.IsNullOrEmpty(notSetName) ? "[Not Set]" : notSetName));

     if (isEditFilter)
         items.Insert(0, new KeyValuePair<string, string>("-1", "All"));
         
            int maxLengthIndex =0;
            for (int i = 0; i < items.Count; i++)
            {
                if (items[maxLengthIndex].Value.Length < items[i].Value.Length)
                {
                    maxLengthIndex = i;
                }
            }

            string maxItemString = items.Count > 0 ? items[maxLengthIndex].Value : "";
       %>
      
      
       <%: Html.Telerik().ComboBox()
                                      .Name(propertyName)
                                      .AutoFill(true)
                                      .SelectedIndex(0)
                                      .BindTo(new SelectList(items, "Key", "Value", Model))
                                      //.HtmlAttributes(new { style = "float:left;"})
                                      .HighlightFirstMatch(true)
                                                                       .ClientEvents(events => events.OnChange("SetInputValue" + propertyName).OnChange("callOnchangeFuction" + propertyName))
                                      .OpenOnFocus(true)
                                      %>

     <div class="t-item" id="invisdiv<%:propertyName %>" style="background-color:Black;color:White;visibility:hidden;display:inline-block;">
        <%:maxItemString%>
    </div>

    <script type="text/javascript">
        var <%:propertyName %>width=$("#invisdiv<%:propertyName %>").last().width();
        if(<%:propertyName %>width==0)
        {
        <%:propertyName %>width=<%:maxItemString.Length%>*10;
        }
       
        SetInputValue<%:propertyName %>();

        $(document).ready(function () {
        
            var comboWidth<%:propertyName %> = <%:propertyName %>width +40;
            $("#<%:propertyName %>").prev().parent().width(comboWidth<%:propertyName %>);

            callOnchangeFuction<%:propertyName %>();
        });
    
    function SetInputValue<%:propertyName %>()
        {
                if($("#<%:propertyName %>-input").val()=="[Not Set]"){
            $("#<%:propertyName %>").val("");
            }
//            if($("#<%:propertyName %>").val()!=""){
//             if("<%:callOnchangeFuction %>"=="True"){
//                <%:onChangeFunctionName %>($("#<%:propertyName %>").val());
//                }
//        }
    }

    function callOnchangeFuction<%:propertyName %>()
        {
            if($("#<%:propertyName %>").val()!=""){
             if("<%:callOnchangeFuction %>"=="True"){
                <%:onChangeFunctionName %>($("#<%:propertyName %>").val());
                }
        }
    }
    </script>
